package test2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2023-03-07
 * Time: 19:25
 */

//饿汉模式 - 静态代码块
public class SingletonTest02 {
    public static void main(String[] args) {
        Singleton singleton = Singleton.getInstance();
        Singleton instance = Singleton.getInstance();
        System.out.println(instance == singleton);
    }
}


class Singleton{
    //1。构造器私有化
    private Singleton(){}

    static {
        //在静态代码块中 创建单例对象
        instance = new Singleton();
    }

    //内部创建对象实例
    private static Singleton instance;

    //3.提供一个共有的静态方法,返回实例对象
    public static Singleton getInstance(){
        return instance;
    }

}